
package com.example.binpacking.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从请求头中获取Token
        String token = request.getHeader("bjToken");

        // 检查Token是否为"123456"
        if ("123456".equals(token)) {
            return true; // 如果Token匹配，则请求通过
        } else {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 设置状态码为401
            response.getWriter().write("Unauthorized222"); // 返回错误信息
            return false; // Token不匹配，请求不通过
        }
    }

}
